#ifndef CLIENTRECVAUDIO_H
#define CLIENTRECVAUDIO_H

#include <QThread>
#include <QObject>
#include <QAudioFormat>
#include <QAudioOutput>
#include <QMutex>
#include <QMutexLocker>
#include <QByteArray>

#include <QtNetwork/QUdpSocket>
#include <QHostAddress>
#include <QNetworkInterface>
#include <QDebug>


class RecvAudio : public QThread
{
    Q_OBJECT
public:
    explicit RecvAudio(QObject *parent = nullptr);
    ~RecvAudio();


    void setCurrentBuffer(QByteArray buffer);
    void addAudioBuffer(char* pData, int len);
    void cleanAllAudioBuffer(void);


    void init(int quality, int port);
    virtual void run(void) override;//多线程重载运行函数run
    void stop();//停止


    void setCurrentVolumn(qreal volumn);
    void setFormat(int quality);

    int test(int a){
        recv_audio_MS+=a;
        if(recv_audio_MS<2)
            recv_audio_MS=2;
        return recv_audio_MS;
    }

private:

    QAudioOutput *m_OutPut = nullptr;
    QIODevice *m_AudioIo = nullptr;
    QAudioFormat format;

    QByteArray m_PCMDataBuffer;
    int m_CurrentPlayIndex = 0;

    QMutex m_Mutex;
    volatile bool m_IsPlaying = true;
    //for Audio
    QUdpSocket *udpsocket;

    int maxAudioLength=960000;
    int frameLen=960;
    int recv_audio_MS=30;

private slots:
    void readyReadSlot();

};

#endif // CLIENTRECVAUDIO_H
